﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using CollegeShop.Api.DBUtility;//Please add references
namespace CollegeShop.Api.Infrastructure
{
	/// <summary>
	/// 数据访问类:t_user
	/// </summary>
	public partial class t_user
	{
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int college, string UserName)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_user");
			strSql.Append(" where UserName=@UserName ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,128)			};
			parameters[0].Value = UserName;

            return DbHelperMySQL.Exists(college, strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(int college, CollegeShop.Api.Entity.t_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_user(");
			strSql.Append("UserName,Password,NickName,RealName,Gender,BirthDay,QQ,Phone,Photo,IsRefuse,IsActivate,Token,SystemRole,DormitoryID,SalesUnitID,IDCardNo,CreateDate)");
			strSql.Append(" values (");
			strSql.Append("@UserName,@Password,@NickName,@RealName,@Gender,@BirthDay,@QQ,@Phone,@Photo,@IsRefuse,@IsActivate,@Token,@SystemRole,@DormitoryID,@SalesUnitID,@IDCardNo,@CreateDate)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,128),
					new MySqlParameter("@Password", MySqlDbType.VarChar,128),
					new MySqlParameter("@NickName", MySqlDbType.VarChar,100),
					new MySqlParameter("@RealName", MySqlDbType.VarChar,100),
					new MySqlParameter("@Gender", MySqlDbType.Int32,11),
					new MySqlParameter("@BirthDay", MySqlDbType.Datetime),
					new MySqlParameter("@QQ", MySqlDbType.VarChar,20),
					new MySqlParameter("@Phone", MySqlDbType.VarChar,50),
					new MySqlParameter("@Photo", MySqlDbType.VarChar,100),
					new MySqlParameter("@IsRefuse", MySqlDbType.Int32,1),
					new MySqlParameter("@IsActivate", MySqlDbType.Int32,1),
					new MySqlParameter("@Token", MySqlDbType.VarChar,100),
					new MySqlParameter("@SystemRole", MySqlDbType.Int32,11),
					new MySqlParameter("@DormitoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@IDCardNo", MySqlDbType.VarChar,30),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.NickName;
			parameters[3].Value = model.RealName;
			parameters[4].Value = model.Gender;
			parameters[5].Value = model.BirthDay;
			parameters[6].Value = model.QQ;
			parameters[7].Value = model.Phone;
			parameters[8].Value = model.Photo;
			parameters[9].Value = model.IsRefuse;
			parameters[10].Value = model.IsActivate;
			parameters[11].Value = model.Token;
			parameters[12].Value = model.SystemRole;
			parameters[13].Value = model.DormitoryID;
			parameters[14].Value = model.SalesUnitID;
			parameters[15].Value = model.IDCardNo;
			parameters[16].Value = model.CreateDate;

			int rows=DbHelperMySQL.ExecuteSql(college, strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(int college, CollegeShop.Api.Entity.t_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_user set ");
			strSql.Append("Password=@Password,");
			strSql.Append("NickName=@NickName,");
			strSql.Append("RealName=@RealName,");
			strSql.Append("Gender=@Gender,");
			strSql.Append("BirthDay=@BirthDay,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Photo=@Photo,");
			strSql.Append("IsRefuse=@IsRefuse,");
			strSql.Append("IsActivate=@IsActivate,");
			strSql.Append("Token=@Token,");
			strSql.Append("SystemRole=@SystemRole,");
			strSql.Append("DormitoryID=@DormitoryID,");
			strSql.Append("SalesUnitID=@SalesUnitID,");
			strSql.Append("IDCardNo=@IDCardNo,");
			strSql.Append("CreateDate=@CreateDate");
			strSql.Append(" where UserName=@UserName ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Password", MySqlDbType.VarChar,128),
					new MySqlParameter("@NickName", MySqlDbType.VarChar,100),
					new MySqlParameter("@RealName", MySqlDbType.VarChar,100),
					new MySqlParameter("@Gender", MySqlDbType.Int32,11),
					new MySqlParameter("@BirthDay", MySqlDbType.Datetime),
					new MySqlParameter("@QQ", MySqlDbType.VarChar,20),
					new MySqlParameter("@Phone", MySqlDbType.VarChar,50),
					new MySqlParameter("@Photo", MySqlDbType.VarChar,100),
					new MySqlParameter("@IsRefuse", MySqlDbType.Int32,1),
					new MySqlParameter("@IsActivate", MySqlDbType.Int32,1),
					new MySqlParameter("@Token", MySqlDbType.VarChar,100),
					new MySqlParameter("@SystemRole", MySqlDbType.Int32,11),
					new MySqlParameter("@DormitoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@IDCardNo", MySqlDbType.VarChar,30),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime),
					new MySqlParameter("@UserName", MySqlDbType.VarChar,128)};
			parameters[0].Value = model.Password;
			parameters[1].Value = model.NickName;
			parameters[2].Value = model.RealName;
			parameters[3].Value = model.Gender;
			parameters[4].Value = model.BirthDay;
			parameters[5].Value = model.QQ;
			parameters[6].Value = model.Phone;
			parameters[7].Value = model.Photo;
			parameters[8].Value = model.IsRefuse;
			parameters[9].Value = model.IsActivate;
			parameters[10].Value = model.Token;
			parameters[11].Value = model.SystemRole;
			parameters[12].Value = model.DormitoryID;
			parameters[13].Value = model.SalesUnitID;
			parameters[14].Value = model.IDCardNo;
			parameters[15].Value = model.CreateDate;
			parameters[16].Value = model.UserName;

			int rows=DbHelperMySQL.ExecuteSql(college, strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool Delete(int college, string UserName)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_user ");
			strSql.Append(" where UserName=@UserName ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,128)			};
			parameters[0].Value = UserName;

			int rows=DbHelperMySQL.ExecuteSql(college, strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public bool DeleteList(int college, string UserNamelist)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_user ");
			strSql.Append(" where UserName in ("+UserNamelist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(college, strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public CollegeShop.Api.Entity.t_user GetModel(int college, string UserName)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserName,Password,NickName,RealName,Gender,BirthDay,QQ,Phone,Photo,IsRefuse,IsActivate,Token,SystemRole,DormitoryID,SalesUnitID,IDCardNo,CreateDate from t_user ");
			strSql.Append(" where UserName=@UserName ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@UserName", MySqlDbType.VarChar,128)			};
			parameters[0].Value = UserName;

			CollegeShop.Api.Entity.t_user model=new CollegeShop.Api.Entity.t_user();
			DataSet ds=DbHelperMySQL.Query(college, strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CollegeShop.Api.Entity.t_user DataRowToModel(DataRow row)
		{
			CollegeShop.Api.Entity.t_user model=new CollegeShop.Api.Entity.t_user();
			if (row != null)
			{
				if(row["UserName"]!=null)
				{
					model.UserName=row["UserName"].ToString();
				}
				if(row["Password"]!=null)
				{
					model.Password=row["Password"].ToString();
				}
				if(row["NickName"]!=null)
				{
					model.NickName=row["NickName"].ToString();
				}
				if(row["RealName"]!=null)
				{
					model.RealName=row["RealName"].ToString();
				}
				if(row["Gender"]!=null && row["Gender"].ToString()!="")
				{
					model.Gender=int.Parse(row["Gender"].ToString());
				}
				if(row["BirthDay"]!=null && row["BirthDay"].ToString()!="")
				{
					model.BirthDay=DateTime.Parse(row["BirthDay"].ToString());
				}
				if(row["QQ"]!=null)
				{
					model.QQ=row["QQ"].ToString();
				}
				if(row["Phone"]!=null)
				{
					model.Phone=row["Phone"].ToString();
				}
				if(row["Photo"]!=null)
				{
					model.Photo=row["Photo"].ToString();
				}
				if(row["IsRefuse"]!=null && row["IsRefuse"].ToString()!="")
				{
					model.IsRefuse=int.Parse(row["IsRefuse"].ToString());
				}
				if(row["IsActivate"]!=null && row["IsActivate"].ToString()!="")
				{
					model.IsActivate=int.Parse(row["IsActivate"].ToString());
				}
				if(row["Token"]!=null)
				{
					model.Token=row["Token"].ToString();
				}
				if(row["SystemRole"]!=null && row["SystemRole"].ToString()!="")
				{
					model.SystemRole=int.Parse(row["SystemRole"].ToString());
				}
				if(row["DormitoryID"]!=null && row["DormitoryID"].ToString()!="")
				{
					model.DormitoryID=int.Parse(row["DormitoryID"].ToString());
				}
				if(row["SalesUnitID"]!=null && row["SalesUnitID"].ToString()!="")
				{
					model.SalesUnitID=int.Parse(row["SalesUnitID"].ToString());
				}
				if(row["IDCardNo"]!=null)
				{
					model.IDCardNo=row["IDCardNo"].ToString();
				}
				if(row["CreateDate"]!=null && row["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(row["CreateDate"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserName,Password,NickName,RealName,Gender,BirthDay,QQ,Phone,Photo,IsRefuse,IsActivate,Token,SystemRole,DormitoryID,SalesUnitID,IDCardNo,CreateDate ");
			strSql.Append(" FROM t_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
        public int GetRecordCount(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperMySQL.GetSingle(college, strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetListByPage(int college, string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UserName desc");
			}
			strSql.Append(")AS Row, T.*  from t_user T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_user";
			parameters[1].Value = "UserName";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

